package dxcorp.dxadminapi.controller;
import com.alibaba.fastjson.JSONObject;
import dxcorp.dxadminapi.annotation.UserLoginToken;
import dxcorp.dxadminapi.common.Constants;
import dxcorp.dxadminapi.common.Result;
import dxcorp.dxadminapi.common.ResultGenerator;
import dxcorp.dxadminapi.entity.User;
import dxcorp.dxadminapi.service.TokenService;
import dxcorp.dxadminapi.service.UserService;
import dxcorp.dxadminapi.utils.PageUtil;
import dxcorp.dxadminapi.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("/admin")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    TokenService tokenService;

    // 登录
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpServletResponse response){
        Result result = ResultGenerator.genFailResult("登录失败");

        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
            result.setMessage("请填写登录aa信息");
        }
        User loginUser = userService.findByUsername(user);
        if (loginUser != null) {
            if(loginUser.getPassword().equals(user.getPassword())){

                JSONObject token = new JSONObject();
                token.put("token",tokenService.getToken(loginUser));
                Cookie cookie = new Cookie("token", tokenService.getToken(loginUser));
                cookie.setPath("/");
                response.addCookie(cookie);
                result = ResultGenerator.genSuccessResult(token);
            }else {
                return result;
            }

        }
        return result;
    }
//    public Object login(User user, HttpServletResponse response) {
//        JSONObject jsonObject = new JSONObject();
//        User myuser = userService.findByUsername(user);
//
//        if(myuser!=null){
//            if (!myuser.getPassword().equals(user.getPassword())) {
//                jsonObject.put("message", "登录失败,密码错误");
//                return jsonObject;
//            } else {
//                String token = tokenService.getToken(myuser);
//                jsonObject.put("token", token);
//                jsonObject.put("code", 20000);
//
//                Cookie cookie = new Cookie("token", token);
//                cookie.setPath("/");
//                response.addCookie(cookie);
//                return jsonObject;
//            }
//        }else {
//            jsonObject.put("message", "用户不存在");
//            return jsonObject;
//        }
//
//    }

    /***
     * 这个请求需要验证token才能访问
     *
     * @author: duxing
     */
    @UserLoginToken
    @GetMapping("/getMessage")
    public Result list(@RequestParam Map<String,Object> params){
        if(TokenUtil.getTokenUserId() != null){
            //查询列表数据
//            PageUtil pageUtil = new PageUtil(params);
//            return ResultGenerator.genSuccessResult(userService.getUserPage(pageUtil));
            return ResultGenerator.genSuccessResult(TokenUtil.getTokenUserId());

        }
        return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_Tokenexpired, "token过期！");
    }
//    public Object getMessage() {
//        // 取出token中带的用户id 进行操作
////        System.out.println(TokenUtil.getTokenUserId());
//        return TokenUtil.getTokenUserId();
////        return "你已通过验证";
//    }

    @UserLoginToken
    @GetMapping("/logout")
    public Object logOut() {
        // 取出token中带的用户id 进行操作
//        System.out.println(TokenUtil.getTokenUserId());
        return TokenUtil.getTokenLogout();
//        return "你已通过验证";
    }
}
